package com.key.custom.plm.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.key.base.action.BaseAction;
import com.key.common.model.Page;
import com.key.custom.plm.model.HrPLM;
import com.key.custom.plm.service.IHrPLMService;
import com.key.util.XspMessage;

public class HrPLMAction extends BaseAction {

	IHrPLMService hrPLMService;
	private HrPLM hrPLM;

	public HrPLM getHrPLM() {
		return hrPLM;
	}

	public void setHrPLM(HrPLM hrPLM) {
		this.hrPLM = hrPLM;
	}

	public IHrPLMService getHrPLMService() {
		if (hrPLMService == null)
			hrPLMService = (IHrPLMService) getBean("hrPLMServiceImp");
		return hrPLMService;
	}

	public void setHrPLMService(IHrPLMService hrPLMService) {
		this.hrPLMService = hrPLMService;
	}

	@Override
	public String save() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// check

			getHrPLMService().add(hrPLM);
		} catch (Exception e) {
			return xspMessage
					.setMessage("保存失败", e.toString(), XspMessage.ERROR);
		}
		return "Success";
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// check

			getHrPLMService().update(hrPLM);
		} catch (Exception e) {
			return xspMessage
					.setMessage("更新失败", e.toString(), XspMessage.ERROR);
		}
		return "Success";
	}

	private Page page;

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	@Override
	public String list() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		String conditional = "";
		String condition = "";
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if (page == null)
				page = new Page();
			// 对象名称
			String classname = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			page.setObjName(classname + " t");
			// 每页显示条数（默认10）
			page.setPageSize(15);
			// 查询属性(为空查询所有)
			if (request.getParameter("partid") != null) {
				String partid = request.getParameter("partid");
				conditional += ("?partid=" + partid);
				condition += " id like '%" + partid + "%' ";
			}
			if (request.getParameter("orgName") != null) {
				String orgName = request.getParameter("orgName");
				if (conditional.equals("")) {
					conditional += ("?orgName=" + orgName);
				} else {
					conditional += ("&orgName=" + orgName);
				}
				if (condition.equals("")) {
					condition += " unitName like '%" + orgName + "%' ";
				} else {
					condition += "and unitName like '%" + orgName + "%' ";
				}
			}
			// 调用工厂获得数据
			page.setCondition(condition);
			page = getPagingFactory().queryHrPart(page);
			request.setAttribute("conditional", conditional);
			request.setAttribute("page", page);
		} catch (Exception e) {
			return xspMessage.setMessage("查看列表失败", e.toString(),
					XspMessage.ERROR);
		}
		return "list";
	}

	@Override
	public String delete() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// 对象名称
			String className = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			getHrPLMService().deleteAllObj(className, getList_Selected());
		} catch (Exception e) {
			return xspMessage
					.setMessage("删除失败", e.toString(), XspMessage.ERROR);
		}
		return "Success";
	}

	@Override
	public String view() {
		XspMessage xspMessage = new XspMessage();
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if (page == null)
				page = new Page();
			// 对象名称
			String classname = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			String id = request.getParameter("id");
			page.setObjName(classname + " t");
			page.setCondition(" id = '" + id + "' ");
			// 查询属性(为空查询所有)
			// 调用工厂获得数据
			page = getPagingFactory().queryHrPart(page);
			String[] names = classname.split("[.]");
			String str = names[names.length - 1];
			str = str.substring(0, 1).toLowerCase() + str.substring(1);
			if (page.getObjList() != null && page.getObjList().size() > 0) {
				request.setAttribute(str, page.getObjList().get(0));
			} else {
				request.setAttribute(str, null);
			}
		} catch (Exception e) {
			return xspMessage
					.setMessage("查看失败", e.toString(), XspMessage.ERROR);
		}
		return "view";
	}

	@Override
	public String edit() {
		XspMessage xspMessage = new XspMessage();
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("methodStyle", "edit");
			if (page == null)
				page = new Page();
			// 对象名称
			String classname = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			String id = request.getParameter("id");
			page.setObjName(classname + " t");
			page.setCondition(" id = '" + id + "' ");
			// 查询属性(为空查询所有)
			// 调用工厂获得数据
			page = getPagingFactory().queryHrPart(page);
			String[] names = classname.split("[.]");
			String str = names[names.length - 1];
			str = str.substring(0, 1).toLowerCase() + str.substring(1);
			if (page.getObjList() != null && page.getObjList().size() > 0) {
				request.setAttribute(str, page.getObjList().get(0));
			} else {
				request.setAttribute(str, null);
			}
		} catch (Exception e) {
			return xspMessage
					.setMessage("编辑失败", e.toString(), XspMessage.ERROR);
		}
		return "edit";
	}
}
